import myAxios from '@/request/request.ts'
import {User, UserAddReq, UserListReq, UserListResp, type UserLoginReq, UserLoginResp} from '@/proto/user.ts'

const API = {
  UserAdd: '/user/add',
  UserList: '/user/list',
  UserLogin: '/user/login',
  UserLoginInfo: '/user/info/login',
  UserUpdate: '/user/update',
  UserDel: '/user/del',
}

// 登录请求
export const login = async (params: UserLoginReq): Promise<UserLoginResp> => {
  return myAxios.post(API.UserLogin, params.serialize()).then((res) => {
    return UserLoginResp.deserialize(res.data)
  })
}

// 注册请求
export const userRegister = async (params: UserAddReq): Promise<void> => {
  return myAxios.post(API.UserAdd, params.serialize())
}

// 查询登录中用户的信息
export const userLoginInfo = async (params: UserLoginReq): Promise<User> => {
  return myAxios.post(API.UserLoginInfo, params.serialize()).then((res) => {
    return User.deserialize(res.data)
  })
}

// 获取用户列表
export const userList = async (params: UserListReq): Promise<UserListResp> => {
  return myAxios.post(API.UserList, params.serialize()).then((res) => {
    return UserListResp.deserialize(res.data)
  })
}
